package com.anxin.controller;

import com.alibaba.fastjson.JSON;
import com.anxin.domain.entity.Product;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author anxinr
 */
@Slf4j
@RestController
@RequestMapping("/es")
public class TestConnectElasticSearchController {
    @Autowired
    private RestHighLevelClient client ;

    /**
     * 尝试链接es并且创建索引
     *
     * @return
     */
    @GetMapping("/connect")
    public String testConnectEs(){
        /**创建索引请求*/
        CreateIndexRequest request = new CreateIndexRequest("product");
        /**执行创建索引请求，并且得到反馈*/
        try {
            /**返回执行结果*/
            CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
            return "链接成功" ;
        }catch (Exception e){
            log.info("请求es创建索引异常 = {}",e);
            e.printStackTrace();
        }
        return "链接失败" ;
    }

    /**
     * 测试获取索引 并且判断是否存在
     * */
    @GetMapping("/exist")
    public String existIndex(String indexName){
        /**创建获取请求*/
        GetIndexRequest request = new GetIndexRequest(indexName);
        /**执行请求 ，获取结果*/
        try {
            boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
            if (exists) {
                return "true" ;
            }else {
                return "false" ;
            }
        }catch (Exception e){
            log.info("请求es异常 = {}",e);
            return "链接失败" ;
        }
    }


    /**
     * 测试获取索引 并且删除
     * */
    @DeleteMapping("/delete")
    public String delete(String indexName){
        /**创建获取请求*/
        DeleteIndexRequest request = new DeleteIndexRequest(indexName);
        /**执行请求 ，获取结果*/
        try {
            AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
            return "true" ;
        }catch (Exception e){
            log.info("请求es异常 = {}",e);
            return "链接失败" ;
        }
    }

    /**
     * 测试获取索引 并且删除
     * */
    @PostMapping("/add")
    public String add(@RequestBody Product product){
        /**创建添加请求*/
        IndexRequest request = new IndexRequest("product");
        /**添加文档规则
         * put /product/_doc/1
         * */
        // index()方法设置索引名；id()方法设置唯一id标识
        request.id("10001").index("product");
        request.timeout(TimeValue.timeValueSeconds(1));
        request.timeout("1s");
        // TODO: 2022/4/25 缺少es依赖注解，不想写字段type，等后期完善es依赖才可开发完成
//        HashMap<String, String> map = new HashMap<>();
//        map.put("id","String");
//        map.put("id","String");
//        map.put("id","String");
        request.source(JSON.toJSONString(product), XContentType.JSON);
        /**发送请求到es*/
        try {
            IndexResponse index = client.index(request, RequestOptions.DEFAULT);
        }catch (Exception e){
            log.info("请求es异常 = {}",e);
            return "添加文档失败" ;
        }
        return "添加文档成功" ;
    }

}

